﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Me.Prettyprint.Hector.Api.Ddl;

namespace Me.Prettyprint.Cassandra.Serializers
{
    /// <summary>
    /// DoubleSerializer handles the conversion between big/little endian
    /// encoding so that things work correctly.
    /// </summary>
    public class DoubleSerializer : AbstractSerializer<double>
    {
        public static DoubleSerializer Instance = new DoubleSerializer();

        public DoubleSerializer()
        {
        }
        public override ComparatorType GetComparatorType()
        {
            return ComparatorType.DOUBLETYPE;
        }
        override public ComparatorType ComparatorType
        {
            get
            {
                return ComparatorType.DOUBLETYPE;
            }
        }
        public override byte[] ToBytes(double obj)
        {
            return ConvertToBigEndianBytes(BitConverter.GetBytes(obj));
        }
        public override Utils.ByteBuffer ToByteBuffer(double obj)
        {
            return new Utils.ByteBuffer(ToBytes(obj));
        }
        public override double FromBytes(byte[] bytes)
        {
            return BitConverter.ToDouble(ConvertFromBigEndianBytes(bytes), 0);
        }
        public override double FromByteBuffer(Utils.ByteBuffer buff)
        {
            return FromBytes(buff.GetBuffer());
        }
    }
}
